package com.angel.web.health;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzPackages;
import com.angel.provider.health.BuzPackagesProvider;

/**
 * 体检套餐
 * @author hanlinxiu
 *
 */
@Controller
@RequestMapping("packages")
public class BuzPackagesController {
	
	@Autowired
	private BuzPackagesProvider buzPackagesService;

	/**
	 * 体检套餐首页
	 * 
	 * @param request
	 * @param buzPackages
	 * @param pager
	 * @return
	 */
	@RequestMapping("/healthPackage")
	public String healthPackage(HttpServletRequest request,@ModelAttribute("buzPackages")BuzPackages buzPackages,Pager pager){
		
		buzPackages.setEnable(1);
		
		pager = buzPackagesService.findAll(buzPackages,pager);
		
		request.setAttribute("pager", pager);
		
		return "institutions/health_package";
	}
	
	/**
	 * 新增页面
	 * 
	 * @param id
	 * @param from
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toAddHealthPackage")
	public String toAddHealthPackage(Integer id, String from, ModelMap modelMap){
		
		BuzPackages buzPackages = null;
		
		if(id != null){
			buzPackages = buzPackagesService.findById(id);
		}
		
		
		modelMap.put("from", from);
		modelMap.put("data", buzPackages);
		
		return "institutions/health_package_add";
	}
	
	
	/**
	 * 新增，更新体检套餐
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addHealthPackage")
	@ResponseBody
	public Map<String, Object> addHealthPackage(@ModelAttribute("user") String user, @ModelAttribute("buzPackages")BuzPackages buzPackages){
		
		Map<String, Object> map = new HashMap<>();
		
		if(buzPackages.getName()==null || buzPackages.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "体检套餐名称不能为空!");
			return map;
		}
		
		if(buzPackages.getId() == null){
			buzPackages.setCreateBy(user);
			buzPackagesService.insert(buzPackages);
		}else{
			buzPackages.setUpdateBy(user);
			buzPackagesService.updateById(buzPackages);
		}
			
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	/**
	 * 删除体检套餐
	 * 
	 * @param buzException
	 * @return
	 */
	@RequestMapping("/deleteHealthPackage")
	@ResponseBody
	public Map<String, Object> deleteHealthPackage(@ModelAttribute("buzPackages")BuzPackages buzPackages){
		Map<String, Object> map = new HashMap<>();
		
		buzPackagesService.delById(buzPackages);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	/**
	 * 批量删除
	 *  
	 * @param checks
	 * @return
	 */
	@RequestMapping("/deleteHealthPackageBatch")
	@ResponseBody
	public Map<String,Object> deleteHealthExceptionBatch(@RequestParam("checks[]")List<Integer> checks){
		
		Map<String, Object> map = new HashMap<>();
		
		buzPackagesService.deleteByKeysSelective(checks);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
}
